package com.bnlr.lrparking.appui.publish.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baidu.mapapi.search.core.PoiInfo;
import com.bnlr.lrparking.R;

import java.util.List;

/**
 * Created by tian on 2017/11/23.
 */

public class SearchPoiAdapter extends RecyclerView.Adapter<SearchPoiAdapter.ViewHolder>{
    private Context context;
    private List<PoiInfo> data;
    private OnPoiItemClickListener onPoiItemClickListener;
    public SearchPoiAdapter(Context context, List<PoiInfo> data) {
        this.context = context;
        this.data = data;
    }

    public void setOnPoiItemClickListener(OnPoiItemClickListener onPoiItemClickListener) {
        this.onPoiItemClickListener = onPoiItemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(context, R.layout.poi_result_layout,null));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tv1.setText(data.get(position).name);
        holder.tv2.setText(data.get(position).address);
        if (position == 0) {
            holder.tv1.setTextColor(Color.parseColor("#00caa3"));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onPoiItemClickListener != null) {
                    onPoiItemClickListener.onItemClick(v,data.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
    public void setNewData (List<PoiInfo> datas) {
        this.data = datas;
        notifyDataSetChanged();
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv1;
        TextView tv2;
        public ViewHolder(View itemView) {
            super(itemView);
            tv1 = (TextView) itemView.findViewById(R.id.tv_poi_1);
            tv2 = (TextView) itemView.findViewById(R.id.tv_poi_2);
        }
    }
    public interface OnPoiItemClickListener {
        void onItemClick (View v, PoiInfo info);
    }
}
